The static keyword in Java primarily functions as a tool for memory management. In Java, the static keyword enables the sharing of variables or methods across multiple instances of a class. Users can apply the static keyword to variables, methods, blocks, and nested classes. Static keyword belongs to the class itself rather than an object of the class. When a variable or method needs to apply to every instance of a class as a constant, the static keyword is employed.
A static member (block, variable, function, or nested
class) must be created by adding the term static before its definition. When a class
member is designated as static, it can be accessed without an object reference and
before the class's objects are created.
The main method of a class is generally
static.
The static keyword is a non-access modifier and can be used for the following in the Java programming language.
A variable is considered static if you declare it to be static keyword before variable.
Problem without Static Keyword
class Company{
int Empid;
String EmpName;
String Company="DockerTpoint";
}
Assuming my company has 2000 employees, each time an object is created, all instance data members will receive RAM. Since each employee has a distinct EmpName and Empid, using an instance data member is appropriate in this situation. The term "Company" here refers to the attribute that all objects share. If we set it to a static, the memory will only be retrieved once for this particular field.
// Java program using static variable
class Emp{ int EmpId; String EmpName; static String CompanyName ="DockerTpoint"; Emp(int EmpId, String EmpName ){ this.EmpId = EmpId; this.EmpName = EmpName; } void display(){ System.out.println("Welcome to DockerTpoint"); System.out.println("Your Empid is "+EmpId+",Your official name is "+EmpName+ " & Your Company name is "+CompanyName+".\n"); } } public class Company{ public static void main(String args[]){ Emp emp1 = new Emp(40139522,"Meena"); Emp emp2 = new Emp(40139524,"Alok"); //we can change the Company of all objects by uncomment below line of code //Emp.Company="ProPoint"; emp1.display(); emp2.display(); } }
Output:
Welcome to DockerTpoint
Your Empid is 40139522,Your Officia name is Meena & Your Company name is DockerTpoint.
Welcome to DockerTpoint
Your Empid is 40139524,Your Officia name is Alok & Your Company name is DockerTpoint.
A method is said to as static when the static keyword is
used in its declaration. The main() function is the most typical illustration of a
static method.It is possible to access any static members without objects of its class.
There are various limitations on methods that are marked static:
// Java program using static variable
class Emp{ int EmpId; String EmpName; static String CompanyName ="DockerTpoint"; Emp(int EmpId, String EmpName ){ this.EmpId = EmpId; this.EmpName = EmpName; } static void CompanyChange(){ CompanyName = "ProPoint"; } void display(){ System.out.println("Welcome to "+CompanyName); System.out.println("Your Empid is "+EmpId+",Your official name is "+EmpName+ " & Your Company name is "+CompanyName+".\n"); } } public class Company{ public static void main(String args[]){ Emp.CompanyChange(); Emp emp1 = new Emp(40139522,"Meena"); Emp emp2 = new Emp(40139524,"Alok"); emp1.display(); emp2.display(); } }
Output:
Welcome to ProPoint
Your Empid is 40139522,Your Officia name is Meena & Your Company name is ProPoint.
Welcome to ProPoint
Your Empid is 40139524,Your Officia name is Alok & Your Company name is ProPoint.
It is due to the fact that calling a static method is not needed of the object. If it were a non-static method, the problem of excessive memory allocation would arise since JVM first constructs an object before calling the main() method.
If a calculation is required to initialise static variables, you can create a static block that will run only once, at class initialization.
// Java program using static variable
class Emp{ int EmpId; String EmpName; static String CompanyName ="DockerTpoint"; Emp(int EmpId, String EmpName ){ this.EmpId = EmpId; this.EmpName = EmpName; } static { System.out.println("Hello, Java static block initialized."); CompanyName = "ProPoint"; // Company Name Changed } void display(){ System.out.println("Welcome to "+CompanyName); System.out.println("Your Empid is "+EmpId+",Your official name is "+EmpName+ " & Your Company name is "+CompanyName+".\n"); } } public class Company{ public static void main(String args[]){ Emp emp1 = new Emp(40139522,"Meena"); Emp emp2 = new Emp(40139524,"Alok"); emp1.display(); emp2.display(); } }
Output:
Welcome to ProPoint
Your Empid is 40139522,Your Officia name is Meena & Your Company name is ProPoint.
Welcome to ProPoint
Your Empid is 40139524,Your Officia name is Alok & Your Company name is ProPoint.
A class can only be made static if it is nested. We cannot use the static modifier on top-level classes, but we can use it on nested classes. Such classes are known as Nested static classes. A reference to the Outer class is not required for a nested static class. In this scenario, a static class cannot access non-static Outer class members.
// Java program using Static Classes
class Emp{ private static String CompanyName = "DockerTpoint"; static class NestedClass { void display(){ System.out.println("Welcome to "+CompanyName); } } } public class Company{ public static void main(String args[]){ Emp.NestedClass object=new Emp.NestedClass(); object.display(); } }
Output:
Welcome to DockerTpoint
Post your comment